package io.renren.modules.avanotes.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzr.common.utils.JsonUtils;
import io.renren.common.utils.BizUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.RedisUtils;
import io.renren.common.vo.ReferenceMaterialVo;
import io.renren.common.vo.input.ReferenceMaterialIUVo;
import io.renren.config.BizConfig;
import io.renren.modules.avanotes.dao.ReferenceMaterialDao;
import io.renren.modules.avanotes.entity.FormworkEntity;
import io.renren.modules.avanotes.entity.ReferenceMaterialEntity;
import io.renren.modules.avanotes.entity.SpecialSubjectEntity;
import io.renren.modules.avanotes.service.ReferenceBankService;
import io.renren.modules.avanotes.service.ReferenceMaterialService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


//@Service("referenceMaterialService")//默认只有一个实现，那就不写名称了直接使用默认的名称！
@Service
public class ReferenceMaterialServiceImpl extends ServiceImpl<ReferenceMaterialDao, ReferenceMaterialEntity> implements ReferenceMaterialService {

    @Autowired
    ReferenceBankService referenceBankService;

    @Autowired
    RedisUtils redisUtils;

    public static final String[] simpleColumnArr = {ReferenceMaterialEntity.CN_ID, ReferenceMaterialEntity.CN_BANK_ID
            , ReferenceMaterialEntity.CN_TITLE};

    @Override
    public ReferenceMaterialEntity getEntity(Integer id) throws Exception {
        String sid = "" + id;
        ReferenceMaterialEntity entity = redisUtils.hGet(BizConfig.CACHE_REFERENCE_MATERIAL_ENTITY, sid, new TypeReference<ReferenceMaterialEntity>(){});
        if (entity == null) {
            //为空，去DB
            entity = getById(id);
            if (entity == null) {
                return null;
            }
            //设置缓存
            redisUtils.hSet(BizConfig.CACHE_REFERENCE_MATERIAL_ENTITY, sid, JsonUtils.parseString(entity));
        }
        return entity;
    }

    @Override
    public List<ReferenceMaterialEntity> getListByScheduleId(Long id) {
        return this.baseMapper.getListByScheduleId(id);
    }

    /**
     * 新增
     * @param vo
     * @return
     */
    @Override
    public Long insert(ReferenceMaterialIUVo vo) {
        ReferenceMaterialEntity entity = new ReferenceMaterialEntity();
        BeanUtils.copyProperties(vo,entity);
        //生成 FileType
        entity.setFileType(BizUtils.genFileType(vo.getPath()));
        this.save(entity);
        return entity.getId();
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ReferenceMaterialEntity> page = this.page(
                new Query<ReferenceMaterialEntity>().getPage(params),
                new QueryWrapper<ReferenceMaterialEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ReferenceMaterialVo> list4SelectByTitle(String keyword) {
//        List<String> res = new ArrayList<>();
        List<ReferenceMaterialEntity> list = this.list(new QueryWrapper<ReferenceMaterialEntity>().select(simpleColumnArr).like(ReferenceMaterialEntity.CN_TITLE, keyword));
        //查询reference bank
        Map<Integer, String> allMap = referenceBankService.getAllMap(1);
        List<ReferenceMaterialVo> resList = new ArrayList<>();
        for (ReferenceMaterialEntity entity : list) {
            //res.add(entity.getTitle() + "(" + allMap.get(entity.getBankId()) + ")");
            String desc = entity.getTitle() + "(" + allMap.get(entity.getBankId()) + ")";
            ReferenceMaterialVo vo = new ReferenceMaterialVo();
            BeanUtils.copyProperties(entity,vo);
            vo.setDesc(desc);

            resList.add(vo);
        }
        return resList;
    }



}